package cn.huoyingkeji.common.handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.huoyingkeji.common.domain.R;
import cn.huoyingkeji.common.exception.SharpknifeException;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @SuppressWarnings("rawtypes")
    @ResponseBody
    @ExceptionHandler(SharpknifeException.class)
    public R exception(SharpknifeException e) {
        e.printStackTrace();
        return R.fail(e.getCode(), e.getMessage());
    }

    @SuppressWarnings("rawtypes")
    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public R exception(RuntimeException e) {
        e.printStackTrace();
        return R.fail(501, "系统运行时异常！");
    }


    @SuppressWarnings("rawtypes")
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R exception(Exception e) {
        e.printStackTrace();
        return R.fail(500, "未知的系统异常，请联系管理员！");
    }
}
